package com.secneo.appshare.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.secneo.appshare.R;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.RemoteApi.Feed;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.Constants;

public class FeedsButtonAdapter extends BaseAdapter {

	private List<Feed> mFeeds;
	private Context mContext;
	private Handler mHandler;
	private String recentUid;
	private String finishedUid;
	private boolean mStoped;
	
	
	public FeedsButtonAdapter(Context ctx, Handler handler, List<Feed> feeds) {
		this.mContext = ctx;
		this.mHandler = handler;
		this.mFeeds = feeds;
		this.mStoped = false;
		
	}

	public void setFeedList(List<Feed> feeds){
		mFeeds = feeds;
	}
	
	public void freeDrawable()
	{
		mStoped = true;
		Log.d("MyTag", "FeedsButton bitmaps free !!! ");
		for(int i=0; i<mFeeds.size(); i++)
		{
			if (!mFeeds.get(i).user_icon.contains("buildin") &&
					mFeeds.get(i).user_icon_drawable != null)
			{
				BitmapDrawable a = (BitmapDrawable) mFeeds.get(i).user_icon_drawable;
				if(a!=null && !a.getBitmap().isRecycled())
					a.getBitmap().recycle();
				mFeeds.get(i).user_icon_drawable = null;
			}
		}
		System.gc();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mFeeds.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mFeeds.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public String getFinishedUid() {
		return finishedUid;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(mStoped)
			return convertView;
		
		FeedItem feedItem;
		final Feed feed = mFeeds.get(position);
		if (convertView == null) {
			View v = LayoutInflater.from(mContext).inflate(R.layout.appshare_user_item, null);
			
			feedItem = new FeedItem();
			feedItem.feedButtonLayout = (FrameLayout)v.findViewById(R.id.feedButtonLayout);
			feedItem.mAppIcon = (ImageView)v.findViewById(R.id.ivAppIcon);
			feedItem.weiboAppIcon = (ImageView)v.findViewById(R.id.weiboAppIcon);
			feedItem.huodongAppIcon = (ImageView)v.findViewById(R.id.huodongAppIcon);
			feedItem.mAppName = (TextView)v.findViewById(R.id.tvAppName);
			feedItem.newIcon = (ImageView)v.findViewById(R.id.is_new) ;
			feedItem.attentButton = (Button)v.findViewById(R.id.attentButton);
			feedItem.attentTag = (TextView)v.findViewById(R.id.attentTag);
			
			
			v.setTag(feedItem);
			convertView = v;
		} else {
			feedItem = (FeedItem)convertView.getTag();
		}
		// set icon
		if (feed.user_icon_drawable == null) 
		{
			feedItem.mAppIcon.setImageResource(R.drawable.some_user);
		} 
		else 
		{
			if(feed.snsType != 0)
			{
				int iconResource = LocalStore.getLocaleStoreInstance().getSmallIconResource(feed.snsType);
				if(iconResource!=0){
					feedItem.weiboAppIcon.setImageResource(iconResource);
					feedItem.weiboAppIcon.setVisibility(View.VISIBLE);
				}
			}
			else
			{
				feedItem.weiboAppIcon.setVisibility(View.INVISIBLE);
			}
			
			//if new flower
			//no need to show new icon with FeedsButtonAdapter
			/*
			if(feed.is_new){
				feedItem.newIcon.setImageResource(R.drawable.new_flower) ;
				feedItem.newIcon.setVisibility(View.VISIBLE) ;
			} else {
				feedItem.newIcon.setVisibility(View.INVISIBLE) ;
			}
			*/

			feedItem.huodongAppIcon.setVisibility(View.INVISIBLE);
			
			feedItem.mAppIcon.setImageDrawable(feed.user_icon_drawable);
		}
		
		// set name
		feedItem.mAppName.setText(feed.user_name);
		feedItem.feedButtonLayout.setVisibility(View.VISIBLE);

		if(1==feed.is_attent){
			feedItem.attentButton.setVisibility(View.GONE);
			feedItem.attentTag.setVisibility(View.VISIBLE);
			
			String title = mContext.getResources().getString(R.string.already_attent);
			feedItem.attentButton.setText(title);
		}
		else{
			feedItem.attentButton.setVisibility(View.VISIBLE);
			feedItem.attentTag.setVisibility(View.GONE);
			
			String title = mContext.getResources().getString(R.string.add_attent);
			feedItem.attentButton.setText(title);
		}

		String uid = LocalStore.getLocaleStoreInstance().getUid();
		if(uid.equals(feed.uid)){
			feedItem.feedButtonLayout.setVisibility(View.INVISIBLE);
		}
		
		feedItem.attentButton.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				Log.e("MyTag", feed.user_name);
				recentUid = feed.uid;
				startAttentThread();
			}
		});
		
		return convertView;
	}

	private void startAttentThread()
	{
		Thread attentThread = new Thread() 
		{
			public void run()
			{
				final String uid = LocalStore.getLocaleStoreInstance().getUid();
				
				String otherUid = recentUid;
				RemoteApiImpl remoteApi = new RemoteApiImpl();
				boolean ret = remoteApi.followSomeone(mContext, otherUid, true);
				
				if(ret)
				{
					finishedUid = otherUid;
					Message msg = new Message();
					msg.what = Constants.MSG_ATTENT_SINGLE_FINISH;
					mHandler.sendMessage(msg);
				}
			}
		};
		attentThread.start();
	}

	class FeedItem {
		FrameLayout feedButtonLayout;
		ImageView mAppIcon;
		ImageView weiboAppIcon;
		ImageView huodongAppIcon;
		ImageView newIcon ;
		TextView mAppName;
		Button attentButton;
		TextView attentTag;
	}
}
